<script setup lang="ts">
import { onMounted, onUnmounted, ref, shallowRef } from 'vue'
import {
  AxesHelper,
  Scene,
  PerspectiveCamera,
  SphereGeometry,
  WebGLRenderer,
  PointsMaterial,
  Points,
  Color,
  AdditiveBlending,
  BufferGeometry,
  BoxGeometry,
  BufferAttribute,
  TextureLoader,
} from 'three'
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls'
import { GUI } from 'dat.gui'
import Stats from 'three/examples/jsm/libs/stats.module'
import snowPng from '../images/14.png'

const contentRef = ref<HTMLCanvasElement>()
const renderer = shallowRef<WebGLRenderer>(new WebGLRenderer())
const orbitControls = shallowRef<OrbitControls>()
const scene = new Scene()
const camera = shallowRef<PerspectiveCamera>()
const axesHelper = new AxesHelper(20)
// 创建性能监视器
let stats = new Stats()

const gui = new GUI()

const init = () => {
  if (contentRef.value) {
    camera.value = new PerspectiveCamera(75, contentRef.value.clientWidth / contentRef.value.clientHeight)
    camera.value.position.set(0, 10, 20)
    scene.add(camera.value)
    scene.add(axesHelper)
    contentRef.value.appendChild(renderer.value.domElement)

    stats.dom.style.position = 'absolute'
    stats.dom.style.top = '0'
    stats.dom.style.left = '0'
    contentRef.value.appendChild(stats.dom)

    renderer.value.setSize(contentRef.value?.clientWidth, contentRef.value?.clientHeight)

    orbitControls.value = new OrbitControls(camera.value, renderer.value.domElement)
    orbitControls.value.enableDamping = true
  }
}

const render = () => {
  if (camera.value) {
    orbitControls.value?.update()
    renderer.value?.render(scene, camera.value)
  }
  stats.update()
  requestAnimationFrame(render)
}

window.addEventListener('resize', () => {
  if (contentRef.value && camera.value) {
    camera.value.aspect = contentRef.value.clientWidth / contentRef.value.clientHeight
    camera.value.updateProjectionMatrix()

    renderer.value.setSize(contentRef.value?.clientWidth, contentRef.value?.clientHeight)
    renderer.value.setPixelRatio(window.devicePixelRatio)
  }
})

onMounted(() => {
  init()

  const textureLoader = new TextureLoader()
  const texture = textureLoader.load(snowPng)
  const count = 1000
  const bufferGeometry = new BufferGeometry()
  const buffer = new Float32Array(count * 3)

  for (let i = 0; i < count; i++) {
    const i3 = i * 3
    buffer[i3] = (0.5 - Math.random()) * 50
    buffer[i3 + 1] = (0.5 - Math.random()) * 50
    buffer[i3 + 2] = (0.5 - Math.random()) * 50
  }
  bufferGeometry.setAttribute('position', new BufferAttribute(buffer, 3))

  const points = new Points(bufferGeometry, new PointsMaterial({ map: texture, alphaMap: texture, transparent: true, depthWrite: false, blending: AdditiveBlending, size: 0.5 }))
  scene.add(points)

  const guiAction = {}

  render()
})

onUnmounted(() => {
  gui.destroy()
})
</script>

<template>
  <div class="snow" ref="contentRef"></div>
</template>

<style lang="scss" scoped>
.snow {
  width: 100%;
  height: 100%;
  position: relative;
}
</style>
